#include "batchsound2txt.h"

BatchSound2Txt::BatchSound2Txt(QObject *parent) : SoundBase(parent)
{

}

QString BatchSound2Txt::getCreateTaskUrl(QString apiId,QString accessToken)
{
    // https://aip.baidubce.com/rpc/2.0/aasr/v1/create
    QString url = QString("https://aip.baidubce.com/rpc/2.0/aasr/v1/create?lan=zh&cuid=%1&token=%2")
            .arg(apiId).arg(accessToken);
//    QString url = QString("https://aip.baidubce.com/rpc/2.0/aasr/v1/create?token=%1")
//            .arg(accessToken);
    qDebug() << "CreateTaskUrl is "<< url;
    return url;
}

void BatchSound2Txt::createTask()
{
    QString strUrl = getCreateTaskUrl(API_id,API_access_token);
    //QString strUrl = URL_CREATE;
    QUrl url;
    url.setUrl(strUrl);
    QNetworkRequest request(url);
    // Head
    //request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");//{'content-type': "application/json"}
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
    // Body
#if 1
    QVariantMap map;
    map["speech_url"] = QString("https://platform.bj.bcebos.com/sdk%2Fasr%2Fasr_doc%2Fdoc_download_files%2F16k.pcm");
    map["format"] = QString("pcm");
    map["pid"] = 80001;
    map["rate"] = 16000;
    //Indented：按照json风格生成字符串，自动缩进
    //Compact：紧凑格式，直接生成一堆字符串,比较挫，但是占用空间小
    QByteArray data = QJsonDocument::fromVariant(map).toJson(QJsonDocument::Indented);
#else
    QJsonObject jsonObj;
    jsonObj.insert("speech_url","https://platform.bj.bcebos.com/sdk%2Fasr%2Fasr_doc%2Fdoc_download_files%2F16k.pcm");
    jsonObj.insert("format","pcm");
    jsonObj.insert("pid",80001);
    jsonObj.insert("rate",16000);
    QJsonDocument document;
    document.setObject(jsonObj);
    QByteArray data = document.toJson(QJsonDocument::Compact);
#endif
    qDebug() << data;
    QByteArray result = post(request,data);
    qDebug() << result;

}

QString BatchSound2Txt::getResult(QString para_API_record_path)
{
    QString ret = "";
//    QFile file(para_API_record_path);
//    if( !(file.open(QIODevice::ReadOnly)))
//    {
//        qWarning("打开语音文件失败！");
//        return ret;
//    }
//    /*读入文件流*/
//    QDataStream in(&file);
//    char *m_buf =new char[file.size()];
//    in.readRawData(m_buf,file.size());
//    file.close();

//    /*发送http请求给百度语音,从而得到语音文本*/ // https://aip.baidubce.com/rpc/2.0/aasr/v1/create
////    QString  getTextUrl = "http://vop.baidu.com/server_api?lan=" + para_API_language + "&cuid=" + para_API_id + "&token=" + para_API_access_token;
//    QString getTextUrl = getRepostUrl(API_id,API_access_token);
//    QUrl url;
//    url.setUrl(getTextUrl);
//    QNetworkRequest request(url);
//    //采用wav格式语音，
//    request.setHeader(QNetworkRequest::ContentTypeHeader, "audio/wav;rate=16000");
//    QByteArray arr = QByteArray(m_buf, file.size());
//    QByteArray result = post(request,arr);
//    ret = parasePostResult(result);
    return ret;
}
